<template>


<table class="table table-bordered">
    <tr>
    <th> <span style="color: red;">*</span>标题</th>
    <th> <input type="text" v-model="Tianjia.gongGao_Name"> </th>
</tr>
<tr>
    <th>公告内容</th>
    <th> <textarea v-model="Tianjia.gongGao_NeiRong"></textarea></th>
</tr>


<tr>
    <th><span style="color: red;">*</span>状态</th>
    <th> <input type="radio" :value="true" v-model="Tianjia.gongGao_State">显示
        <input type="radio" :value="false" v-model="Tianjia.gongGao_State">隐藏
    
    </th>
</tr>
<tr>
    <th><span style="color: red;">*</span>类型</th>
    <th> <select v-model="Tianjia.type_Id">
    <option :value="-1">--请选择---</option>
    <option v-for="item in XiaLa" :value="item.type_Id">{{ item.type_Name }}</option>
        </select>
            
    
    </th>
</tr>


<tr>
    <th></th>
    <th>
        <button  @click="TianJIa1">添加</button>
    </th>
</tr>

</table>


</template>

<script setup lang="ts">
import { onMounted,ref } from 'vue';
import axios from 'axios';

import { useRouter ,createRouter} from 'vue-router';
onMounted(()=>{  //钩子函数
    XiaLa1();

})

const XiaLa=ref([{    //下拉的响应式函数
 
 "type_Id": 1,
    "type_Name": "服务协议"


}])

const XiaLa1=()=>{   //下拉的方法


    axios.get('https://localhost:7151/api/GongGao/ShowType').then(res=>{

        XiaLa.value=res.data;

    })
}


const Tianjia =ref({ //添加的响应式函数
"gongGao_Id": 0,
  "gongGao_Name": "",
  "gongGao_Fabu": "",
  "type_Id": -1,
  "gongGao_NeiRong": "",
  "gongGao_State": "",
  "gongGao_Time": "2025-01-06T04:20:18.383Z"


})

const router=useRouter();


const Zhengze=/^[\u4e00-\u9fa5]{1,20}$/;

const TianJIa1=()=>{  //添加方法



if(Tianjia.value.gongGao_Name=="")
{
    alert('姓名不能为空')
    return ;
}

    if(!Zhengze.test(Tianjia.value.gongGao_Name))
{
alert('姓名必须为中文切低于20字')
return ;
}



if(Tianjia.value.gongGao_NeiRong.length>=50)
{
    alert('公告内容必须低于50字')
    return ;
}
if(Tianjia.value.gongGao_State=="")
{
    alert('请选择状态')
   return ;
}
if(Tianjia.value.type_Id==-1)
{
    alert('请选择类型')
   return ;
}

    axios.post('https://localhost:7151/api/GongGao/AddGongGao',Tianjia.value).then(res=>{


if(res.data>0)
{
    alert('添加成功')
router.push('/ShowGongGao')

}
else
{
    alert('添加失败')
}

    })
}




</script>